package cn.huiyunche.driver.controller.ils;


import cn.huiyunche.base.service.interfaces.DriverBillDetailsService;
import cn.huiyunche.base.service.model.DDriverBillDetails;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 司机账单详情导入
 * Created by Felix on 2019/9/19.
 */
@Controller
@RequestMapping("/driver_bill_details")
public class DriverBillDetailsController {

    private static final Logger logger = LoggerFactory.getLogger(DriverBillDetailsController.class);

    @Autowired
    private DriverBillDetailsService driverBillDetailsService;

    /**
     * 导入excel文件
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/import", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<List<DDriverBillDetails>> uploadExcel(@RequestParam("excelData") String excelData) throws Exception {
        logger.info("uploadExcel start: DriverBillDetailsController job time: {}", new Date().toString());
        Result<List<DDriverBillDetails>> result = new Result<>(true, "导入成功!");

            if (StringUtils.isEmpty(excelData)){
                logger.error("excel数据异常");
                result.setSuccess(false);
                throw new BusinessException("excel数据异常");
            }else {
                List<DDriverBillDetails> list = new ArrayList<DDriverBillDetails>();
                try {
                list = JSONObject.parseArray(excelData, DDriverBillDetails.class);
                } catch (Exception e) {
                    logger.error("DriverBillDetailsController.uploadExcel error: {}", e);
                    result.setSuccess(false);
                    throw new BusinessException(e.getMessage());
                }
                if (list.size() == 0){
                    logger.error("excel数据为空");
                    result.setSuccess(false);
                    throw new BusinessException("excel数据为空");
                }else{
                    try {
                        String batchId = UUID.randomUUID().toString();
                        result.setData(driverBillDetailsService.importExcel(list,batchId));
                    } catch (Exception e) {
                        logger.error("DriverBillDetailsController.uploadExcel error: {}", e);
                        result.setSuccess(false);
                        throw new BusinessException(e.getMessage());
                    }
                }
            }

        return result;
    }


    @RequestMapping(value = "/loadExcelData", method = RequestMethod.GET)
    public
    @ResponseBody
    Result<Object> listByPage(PageVo pageVo, @RequestParam("batchId") String batchId) throws Exception {
        logger.info("DriverBillDetailsController.listByPage params : {}, {}", pageVo);
        Result<Object> result = new Result<>(true, null, "查询成功!");
        try {
            result.setData(driverBillDetailsService.selectListByConditions(pageVo, batchId));
        } catch (Exception e) {
            logger.error("DriverBillDetailsController.listByPage error: {}", e);
            throw new BusinessException("加载数据失败!");
        }
        return result;
    }
}
